<?php

/**
 * 获取账号类型
 * 2  物流公司 3消费机构  4管理员
 */
function get_user_model(){
    $admin_id=session('ADMIN_ID');
    $user = M("users")->where(array("id"=>$admin_id))->find();
    if(empty($user)){
        die("账户信息获取失败!");
    }
    return $user['model'];
}

function is_manager($user_model){
    return in_array($user_model, array('1', '4', '5', '8', '9', '10', '11', '12', '13'));
}

/**
 * 获取账号信息
 */
function get_user_role(){

    $admin_id = session('ADMIN_ID');
    $user = M("users")->where(array("id"=>$admin_id))->find();
    $role = M("role")->where(array("id"=>$user['model']))->find();
    
    if(empty($user)){
        die("账户信息获取失败!");
    }
    
    return $role?$role:array('id'=>0);
}

/**
 * 获取物流公司ID
 */
function get_logistics_id(){
	$admin_id=session('ADMIN_ID');
	$user = M("users")->where(array("id"=>$admin_id,'model'=>array('in','2,7')))->find();
    if(empty($user)){
        return false;
    }
	$lid = $user['organize_id'];
	return $lid;
}


/**
 * 获取物流公司ID
 */
function get_logistics_ids($lid = false){
    if (!$lid) {
        $admin_id = session('ADMIN_ID');
        $user = M("users")->where(array("id"=>$admin_id,'model'=>array('in','2,7')))->find();
        if(empty($user)){
            return false;
        }
        $lid = $user['organize_id'];
    }
    
    $lid_child[] = $lid;
    $lid_child_query = M("Logistics")->where(array("manager_id"=>$lid))->field('id')->select();
    if($lid_child_query) foreach ($lid_child_query as $_key => $_row) {
        $lid_child[] = $_row['id'];
    }
    $lid_child = implode(',', $lid_child);

    return $lid_child;
}

/**
 * 查看物流公司信息
 */
function get_logistics_data(){
	$lid = get_logistics_id();
	if(empty($lid)){
		die("物流公司信息查询失败");
	}
	$logistics = M("Logistics")->where(array("id"=>$lid))->find();
	return $logistics;
}

/**
 * 获取config值
 */
function get_config_by_key($key){
	$config = M("Config")->where(array("key"=>$key))->find();
	return $config['value'];
}

/**
 * 获取消费机构ID
 */
function get_sale_id(){
    $admin_id=session('ADMIN_ID');
    $user = M("users")->where(array("id"=>$admin_id,'model'=>3))->find();
    $lid = $user['organize_id'];
    return $lid;
}

/**
 * 查看物流公司信息
 */
function get_sale_data(){
    $sale_id = get_sale_id();
    if(empty($sale_id)){
        die("消费机构信息查询失败！");
    }
            
    $sale = M("Sale")->field('s.*, sm.sale_manage_name, sm.balance as upper_balance, sm.give_percent')->alias('s')->join("bdt_sale_manager sm ON sm.id = s.sale_manager_id","LEFT")->where(array("s.id"=>$sale_id))->find();
    return $sale;
}

/**
 * 获取物流公司的所有卡片ID
 */
function get_card_id_by_logistics(){
	$card_list = array();
	$lid = get_logistics_id();
    if($lid){
        $card_arr = M("Card")->where(array("lid"=>$lid))->select();
    }else{
        return array();
        // $card_arr = M("Card")->select();
    }

	foreach($card_arr AS $citem){
		$card_list[] = $citem['id'];
	}
	return $card_list;
}


